import os
 
from google.appengine.ext import webapp
from django.utils import translation
from cookies import Cookies
import logging
from google.appengine.ext.webapp import template


class I18NHandler(webapp.RequestHandler):
  def render_template(self, file, template_args):
    path = os.path.join(os.path.dirname(__file__), file)
    self.response.out.write(template.render(path, template_args))
  def initialize(self, request, response):
    webapp.RequestHandler.initialize(self, request, response)
    self.request.COOKIES = Cookies(self)
    self.request.META = os.environ
    self.reset_language()
  def reset_language(self):
    # Decide the language from Cookies/Headers
    language = translation.get_language_from_request(self.request)
    logging.fatal(language)
    translation.activate(language)
    self.request.LANGUAGE_CODE = translation.get_language()
    logging.fatal(self.request.LANGUAGE_CODE);
    # Set headers in response
    self.response.headers['Content-Language'] = translation.get_language()           
#    translation.deactivate()
    




